<?php
namespace Admin\Controller;
//工具类
use Admin\Common\Tools as T;
//逻辑类
use Admin\Common\Logic;
use Admin\Common\Api\UserApi;
use Think\Controller;
use Think\Model;


/*require('/Admin/Common/Api/UserApi.class.php');*/

class AbcController extends Controller {
private $HAND_LOGINC   = null;

 protected function _initialize(){
    $this->HAND_LOGINC = new Logic();

 }

    /**
     * 权限验证方法
     * @param $uid
     * @return bool
     */
    public  function  index(){
        // 允许 fizzday.net 发起的跨域请求
        header("Access-Control-Allow-Origin: fizzday.net"); 

        //如果需要设置允许所有域名发起的跨域请求，可以使用通配符 *
        header("Access-Control-Allow-Origin: *");
        $model =D("admin");
        $data = $this->_list($model,$map);
        //$json_data = json_encode($data);
        $this->ajaxReturn($data);
      // echo $json_data;
    }



public function get_client_ip($type = 0) {
    $type       =  $type ? 1 : 0;
    static $ip  =   NULL;
    if ($ip !== NULL) return $ip[$type];
    if($_SERVER['HTTP_X_REAL_IP']){//nginx 代理模式下，获取客户端真实IP
        $ip=$_SERVER['HTTP_X_REAL_IP'];     
    }elseif (isset($_SERVER['HTTP_CLIENT_IP'])) {//客户端的ip
        $ip     =   $_SERVER['HTTP_CLIENT_IP'];
    }elseif (isset($_SERVER['HTTP_X_FORWARDED_FOR'])) {//浏览当前页面的用户计算机的网关
        $arr    =   explode(',', $_SERVER['HTTP_X_FORWARDED_FOR']);
        $pos    =   array_search('unknown',$arr);
        if(false !== $pos) unset($arr[$pos]);
        $ip     =   trim($arr[0]);
    }elseif (isset($_SERVER['REMOTE_ADDR'])) {
        $ip     =   $_SERVER['REMOTE_ADDR'];//浏览当前页面的用户计算机的ip地址
    }else{
        $ip=$_SERVER['REMOTE_ADDR'];
    }
    // IP地址合法验证
    $long = sprintf("%u",ip2long($ip));
    $ip   = $long ? array($ip, $long) : array('0.0.0.0', 0);
    return $ip[$type];
}



   /**
     * 添加
     */
    public  function  insert_admin(){
        //如果需要设置允许所有域名发起的跨域请求，可以使用通配符 *
        header("Access-Control-Allow-Origin: *");
        header("Access-Control-Allow-Headers: x-requested-with,content-type");
        header('Access-Control-Allow-Methods: OPTIONS,POST,GET');
            $data["name"]=I("request.name");
            $data["pwd"]=I("request.pwd");
            $data["power"]=I("request.power");
            $data["add_time"]=date('Y-m-d H:i:s');
            $data["last_login_time"]=date('Y-m-d H:i:s');
            $data["last_login_ip"]=$this.get_client_ip();
            $data["is_activity"]=1;
      
            $auth_ruleModel=M("Admin");
            $rowid=$auth_ruleModel->data($data)->add();
            if($rowid){
                $this->ajaxReturn(array('status'=>1,'info'=>'添加成功！'));
            }else{
                $this->ajaxReturn(array('status'=>1,'info'=>'添加失败！'));
            }

    }

       /**
     * 添加
     */
    public  function  update_admin(){
        //如果需要设置允许所有域名发起的跨域请求，可以使用通配符 *
        // header("Access-Control-Allow-Origin: *");
        // header("Access-Control-Allow-Headers: x-requested-with,content-type");
        // header('Access-Control-Allow-Methods: OPTIONS,POST,GET');
        
            // print_r($_REQUEST);exit;
            // print_r(@file_get_contents('php://input'));exit;
            $id=I("request.id");
            $data["name"]=I("request.name");
            $data["pwd"]=I("request.pwd");
            $data["power"]=I("request.power");
            $datas = @file_get_contents('php://input');
            $json_data = json_decode($datas,true);
            if(!empty($json_data)){
               $id = $json_data['id'];
               $data['name'] = $json_data['name'];
               $data['pwd'] = $json_data['pwd'];
               $data['power'] = $json_data['power'];

            }
            $data["add_time"]=date('Y-m-d H:i:s');
            $data["last_login_time"]=date('Y-m-d H:i:s');
            $data["last_login_ip"]=$this.get_client_ip();
            $data["is_activity"]=1;
            $auth_ruleModel=M("Admin");
            $rowid=$auth_ruleModel->where(array("id"=>$id))->save($data);
            if($rowid>0||$rowid===0){
                $this->ajaxReturn(array('status'=>1,'info'=>'修改成功！'));

            }else{
                $this->ajaxReturn(array('status'=>1,'info'=>'修改失败！'));
            }
    }
     public  function delete_admin(){
                // 允许 fizzday.net 发起的跨域请求
        header("Access-Control-Allow-Origin: fizzday.net"); 

        //如果需要设置允许所有域名发起的跨域请求，可以使用通配符 *
        header("Access-Control-Allow-Origin: *");
        $id=I("request.id");
        $auth_ruleModel=M("admin");
        $auth_ruleModel->where(array("id"=>$id))->delete();
        // $this->success("删除成功！");
         $this->ajaxReturn(array('status'=>1,'info'=>'删除成功！'));
  }

    public function _list($model,$map,$sortBy='',$asc = false){
        //排序
        if(isset($_REQUEST['_order'])){
            $order = $_REQUEST['_order'];
        }else{
            $order = !empty($sortBy)?$sortBy:$model->getPk();
        }
        //排序方式，
        if(isset($_REQUEST["_sort"])){
            $sort = $_REQUEST["_sort"]?'asc':'desc';
        }else{
            $sort = $asc?'asc':'desc';
        }
        $voList = null;
        //取出满足条件的记录数据
        $count = $model->where($map)->count($model->getPk());
        if($count>0){
            //创建分页对象，是否设置行数
            if(!empty($_REQUEST['rows'])){
                $pageSize = $_REQUEST['rows'];
            }else{
                $pageSize = "10";
            }
            if(!empty($_REQUEST['page'])){
                $pageNow = $_REQUEST['page'];
            }else{
                $pageNow = "1";
            }
            $offset = $pageSize * ($pageNow - 1); //定义偏移量
            //分页查询出数据
            $voList = $model->where($map)->order($order." ".$sort)->limit($offset." , ".$pageSize)->select();

        }
        return array("total"=>$count,"rows"=>$voList);
    }



 
}
